package com.example.hp.ormapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;

import com.example.hp.ormapp.MainActivity;
import com.example.hp.ormapp.base.BaseActivity;
import com.example.hp.ormapp.util.NetWorkUtil;
import com.gyf.barlibrary.BarHide;
import com.gyf.barlibrary.ImmersionBar;
import com.rongsheng.ormapp.R;
import com.scwang.wave.MultiWaveHeader;

import java.lang.ref.WeakReference;

/**
 * Created by hang on 2018/4/24 0024.
 */

public class LaunchActivity extends BaseActivity {
    private LauncherHandler launcherHandler;
    private MultiWaveHeader mLaunchWaveHeader;

    @Override
    public int getLayoutId() {
        return R.layout.activity_launch;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        launcherHandler = new LauncherHandler(this);
        mLaunchWaveHeader = (MultiWaveHeader) findViewById(R.id.launch_waveHeader);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onStart() {
        super.onStart();
        showCheckNetState();
    }

    /**
     * 页面跳转
     */
    private class LauncherHandler extends Handler {
        WeakReference<LaunchActivity> launcherReference;

        LauncherHandler(LaunchActivity activity) {
            launcherReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (launcherReference.get() != null) {
                startActivity(new Intent(LaunchActivity.this, MainActivity.class));
                finish();
            }
        }
    }

    /**
     * 检查网络状态
     */
    private void showCheckNetState() {
        if (!NetWorkUtil.isNetworkConnected(this)) {
            toast("无网络连接");
            launcherHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            launcherHandler.sendEmptyMessageDelayed(0, 2000);
        }
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_STATUS_BAR).init();
    }

    @Override
    protected boolean enableSliding() {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mLaunchWaveHeader.isRunning()) {
            mLaunchWaveHeader.stop();
        }
    }
}
